import { Message } from "element-ui";
export function tipErr(msg) {
  Message.close();
  Message.error({
    message: msg,
    offset: 300,
    duration: 2000,
  });
}
export function tipSuc(msg) {
  Message.close();
  Message.success({
    message: msg,
    offset: 300,
    duration: 2000,
  });
}
export function tipWarn(msg) {
  Message.close();
  Message.warning({
    message: msg,
    offset: 300,
    duration: 2000,
  });
}

/*
 * 设置setLocalStorage
 * */
export function setLocalStorage(key, value) {
  window.localStorage.setItem(key, window.JSON.stringify(value));
}
/*
 * 获取getLocalStorage
 * */
export function getLocalStorage(key) {
  return window.JSON.parse(window.localStorage.getItem(key) || "[]");
}
/*
 * 设置setSessionStorage
 * */
export function setSessionStorage(key, value) {
  window.sessionStorage.setItem(key, window.JSON.stringify(value));
}

/*
 * 获取getSessionStorage
 * */
export function getSessionStorage(key) {
  return window.JSON.parse(window.sessionStorage.getItem(key) || "[]");
}

/*
 * 格式化时间
 * yyyy-MM-dd hh:mm:ss
 * */
export function formatDate(date, fmt) {
  if (!date || date == null) return null;
  let format = fmt || "yyyy-MM-dd hh:mm:ss";

  let dates = new Date(date);

  let formatObj = {
    y: dates.getFullYear(),
    M: dates.getMonth() + 1,
    d: dates.getDate(),
    h: dates.getHours(),
    m: dates.getMinutes(),
    s: dates.getSeconds(),
  };
  let time_str = format.replace(/(y|M|d|h|m|s)+/g, (result, key) => {
    let value = formatObj[key];
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}
